package com.kdd.campus.sh.service.impl;

import com.github.pagehelper.PageHelper;
import com.kdd.campus.sh.dao.ShRecordMapper;
import com.kdd.campus.sh.po.ShRecord;
import com.kdd.campus.sh.service.ShRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @Auther: fcp
 * @Date: 2020/4/1 16:26
 * @Description:
 */
@Service
public class ShRecordServiceImpl implements ShRecordService {
    @Autowired
    private ShRecordMapper recordMapper;

    /**
     * 查询最新的几条历史记录
     * @param userId
     * @param pageSize
     * @return
     */
    @Override
    public List<ShRecord> findHistory(String userId, int pageSize) {
        PageHelper.startPage(0, pageSize);
        Example example = new Example(ShRecord.class);
        example.createCriteria().andEqualTo("userId" , userId);
        example.setOrderByClause("modified DESC");
        return recordMapper.selectByExample(example);
    }

    @Override
    public void deleteHistory(Long historyId) {
        // 假删除：修改状态以及modified(最近一次修改的时间)
        ShRecord shRecord = ShRecord.builder().status(0).id(historyId).modified(new Date()).build();
        recordMapper.updateByPrimaryKeySelective(shRecord);
    }

    @Override
    public void emptyHistory(Long userId) {
        ShRecord shRecord = ShRecord.builder().modified(new Date()).status(0).build();

        Example example = new Example(ShRecord.class);
        example.createCriteria()
                .andEqualTo("userId",userId)
                .andEqualTo("status", 1);

        recordMapper.updateByExampleSelective(shRecord,example);
    }
}
